<?php
/**
 * Created by PhpStorm.
 * User: dongxiaofeng
 * Date: 2017/3/16
 * Time: 15:56
 */
namespace zhtfrontend\modules\sitepadapi\controllers;

use common\components\extenders\CheckoutManage;
use common\components\helpers\Fun;
use common\components\helpers\Tree;
use common\components\helpers\Util;
use common\models\datas\Cate;
use common\models\datas\DepositLog;
use EasyWeChat\Payment\Order;
use zhtfrontend\models\datas\ClassLesson;
use zhtfrontend\models\datas\ClassManage;
use zhtfrontend\models\datas\ClassStudent;
use zhtfrontend\models\datas\Course;
use zhtfrontend\models\datas\Goods;
use zhtfrontend\models\datas\GoodsAttr;
use zhtfrontend\models\datas\GoodsClassTime;
use zhtfrontend\models\datas\Orders;
use zhtfrontend\models\datas\StudentCheck;
use zhtfrontend\models\datas\Teacher;
use zhtfrontend\modules\sitepadapi\components\rest\ActiveController;
use yii\data\ArrayDataProvider;
use yii\db\Query;
use yii\filters\Cors;
use yii\web\Response;

class SearchController extends ActiveController
{
    public $modelClass = 'zhtfrontend\models\datas\Goods';

    public $serializer = [
        'class' => 'zhtfrontend\modules\sitepadapi\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        $behaviors['corsFilter'] = [
            'class' => Cors::className(),
            'cors' => [
                'Origin' => ['*'],
                'Access-Control-Allow-Origin' => ['*'],
                'Access-Control-Request-Method' => ['POST', 'PUT', 'GET', 'HEAD', 'OPTIONS'],
                'Access-Control-Request-Headers' => ['Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With'],
                'Access-Control-Allow-Credentials' => true,
                'Access-Control-Max-Age' => 3600,
                'Access-Control-Expose-Headers' => ['X-Pagination-Current-Page'],
            ],
        ];
        return $behaviors;
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionCourse($teacher_id,$role='teache_teacher'){
//        $teacher_id = \Yii::$app->request->get('teacher_id');
        $post = \Yii::$app->request->post();
        if($teacher_id == null){
            return ['statusCode'=>200,'message'=>'error','items'=>'请选登录'];
        }
        if($role == 'class_teacher'){
            $teacher_ids = (new Query())->select('id')->from('{{%teacher}}')->where(['parent_id'=>$teacher_id])->column(Fun::getSiteDb());
//            foreach ($teacher_ids as $tid){
//
//            }
            $return = [];
            $goodsarr_query = (new Query())->select(["goods_id","max(IF(name='地域',value,'')) as region1","max(IF(obj_type='teacher',attribute_id,'')) as teacher_id","max(IF(obj_type='teacher',value,'')) as teacher_name",
                "max(IF(name='是否可预约',value,'')) as is_appointment","max(IF(name='授课方式',value,'')) as teacher_form","max(IF(name='最满人数',value,'')) as max_number",
                "max(IF(name='上课次数',value,'')) as class_count","max(IF(obj_type='suitable_age',value,'')) as suitable_age"])->from('{{%goods_attr}}')->groupBy('goods_id');
            $goods_query = (new Query())->select('g.*,goodsarr.teacher_name as teacher_name')->from('{{%goods}} g')->leftJoin(['goodsarr'=>$goodsarr_query],'goodsarr.goods_id=g.id')
                ->leftJoin('{{%goods_class_time}} gct','gct.goods_id=g.id')
                ->where(['goodsarr.teacher_id'=>$teacher_ids,'g.is_delete'=>0]);//上下架的条件先去了吧 我也不知道要不要留为了测试多点数据就去了'g.is_shelve'=>1,
            if(isset($post['teacher_form']) && $post['teacher_form'] != '' &&  $post['teacher_form'] != 'all'){
                $goods_query->andWhere(['goodsarr.teacher_form'=>$post['teacher_form']]);
            }
            if(!empty($post['class_day'])){
                $goods_query->andWhere(['gct.class_day'=>$post['class_day']]);
            }


            $category_all = Fun::cache('CateGoodsCourse_cache',false);
            $cat_ids = array_keys($category_all);
            if(!empty($post['cat_id']) && $post['cat_id'] != 'all'){
                $category = Cate::findOne(['id' => $post['cat_id']]);
                $category_childs = $category['arrchild_id'];
                $category_childsarr = explode(",", $category_childs);
                foreach ($category_childsarr as $k => $vv) {
                    if (!in_array($vv, $cat_ids)) {
                        unset($category_childsarr[$k]);
                    }
                }
                $goods_query->andWhere(['in','g.cat_id',$category_childsarr]);
            }else{
                $category_childsarr = $cat_ids;
            }
            $goods_query->andWhere(['in','g.cat_id',$category_childsarr]);

            if(!empty($post['content'])){
                $goods_query->andWhere(['like', 'g.name', $post['content']]);
            }

            $coures_list =$goods_query->groupBy('g.id')->all(Fun::getSiteDb());
            if($coures_list){
                foreach ($coures_list as $val){
                    $d = [];
                    $d['goods_id'] = $val['id'];
                    $d['goods_name'] = $val['name'];
                    $d['teacher_name'] = $val['teacher_name'];
                    $d['price'] = $val['price'];
                    $d['picture'] = $val['picture'];
                    $goods_group = (new Query())->select('*')->from('{{%goods_group}}')->where(['goods_id'=>$val['id']])->one(Fun::getSiteDb());
                    if(!empty($goods_group)){
                        $art_courses = (new Query())->select('a.*,c.name as course_name,c.description as course_description')->from('{{%arrangement}} a')
                            ->leftJoin('{{%art_relation}} ar','a.id = ar.arrangement_id')
                            ->leftJoin('{{%course}} c','c.id = a.course_id')
                            ->where(['ar.art_teaching_id'=>$goods_group['group_id']])->all(Fun::getSiteDb());
                        $d['art_courses'] = $art_courses;
                    }else{
                        $d['art_courses'] = null;
                    }
                    $return[] = $d;
                }
            }
            return new ArrayDataProvider([
                'allModels' => $return,
                'pagination' => [
                    'pageSize' => 6
                ],
            ]);
        }else{
            $teacher = Teacher::findOne($teacher_id);
            $return = [];
            $goodsarr_query = (new Query())->select(["goods_id","max(IF(name='地域',value,'')) as region1","max(IF(obj_type='teacher',attribute_id,'')) as teacher_id",
                "max(IF(name='是否可预约',value,'')) as is_appointment","max(IF(name='授课方式',value,'')) as teacher_form","max(IF(name='最满人数',value,'')) as max_number",
                "max(IF(name='上课次数',value,'')) as class_count","max(IF(obj_type='suitable_age',value,'')) as suitable_age"])->from('{{%goods_attr}}')->groupBy('goods_id');
            $goods_query = (new Query())->select('g.*')->from('{{%goods}} g')->leftJoin(['goodsarr'=>$goodsarr_query],'goodsarr.goods_id=g.id')
                ->leftJoin('{{%goods_class_time}} gct','gct.goods_id=g.id')
                ->where(['goodsarr.teacher_id'=>$teacher_id,'g.is_delete'=>0]);//上下架的条件先去了吧 我也不知道要不要留为了测试多点数据就去了'g.is_shelve'=>1,
            if(isset($post['teacher_form']) && $post['teacher_form'] != '' &&  $post['teacher_form'] != 'all'){
                $goods_query->andWhere(['goodsarr.teacher_form'=>$post['teacher_form']]);
            }
            if(!empty($post['class_day'])){
                $goods_query->andWhere(['gct.class_day'=>$post['class_day']]);
            }


            $category_all = Fun::cache('CateGoodsCourse_cache',false);
            $cat_ids = array_keys($category_all);
            if(!empty($post['cat_id']) && $post['cat_id'] != 'all'){
                $category = Cate::findOne(['id' => $post['cat_id']]);
                $category_childs = $category['arrchild_id'];
                $category_childsarr = explode(",", $category_childs);
                foreach ($category_childsarr as $k => $vv) {
                    if (!in_array($vv, $cat_ids)) {
                        unset($category_childsarr[$k]);
                    }
                }
                $goods_query->andWhere(['in','g.cat_id',$category_childsarr]);
            }else{
                $category_childsarr = $cat_ids;
            }
            $goods_query->andWhere(['in','g.cat_id',$category_childsarr]);

            if(!empty($post['content'])){
                $goods_query->andWhere(['like', 'g.name', $post['content']]);
            }

            $coures_list =$goods_query->groupBy('g.id')->all(Fun::getSiteDb());
            if($coures_list){
                foreach ($coures_list as $val){
                    $d = [];
                    $d['goods_id'] = $val['id'];
                    $d['goods_name'] = $val['name'];
                    $d['teacher_name'] = $teacher->name;
                    $d['price'] = $val['price'];
                    $d['picture'] = $val['picture'];
                    $return[] = $d;
                }
            }
            return new ArrayDataProvider([
                'allModels' => $return,
                'pagination' => [
                    'pageSize' => 6
                ],
            ]);
        }


    }

    //针对管理员的课程搜索
    public function actionCourseforadmin(){
//        $teacher_id = \Yii::$app->request->get('teacher_id');
        $post = \Yii::$app->request->post();
        $return = [];
        $goodsarr_query = (new Query())->select(["goods_id","max(IF(name='地域',value,'')) as region1","max(IF(obj_type='teacher',attribute_id,'')) as teacher_id","max(IF(obj_type='teacher',value,'')) as teacher_name",
            "max(IF(name='是否可预约',value,'')) as is_appointment","max(IF(name='授课方式',value,'')) as teacher_form","max(IF(name='最满人数',value,'')) as max_number",
            "max(IF(name='上课次数',value,'')) as class_count","max(IF(obj_type='suitable_age',value,'')) as suitable_age"])->from('{{%goods_attr}}')->groupBy('goods_id');
        $goods_query = (new Query())->select('g.*,goodsarr.teacher_name as teacher_name')->from('{{%goods}} g')->leftJoin(['goodsarr'=>$goodsarr_query],'goodsarr.goods_id=g.id')
            ->leftJoin('{{%goods_class_time}} gct','gct.goods_id=g.id');//上下架的条件先去了吧 我也不知道要不要留为了测试多点数据就去了'g.is_shelve'=>1,
        if(isset($post['teacher_form']) && $post['teacher_form'] != '' &&  $post['teacher_form'] != 'all'){
            $goods_query->andWhere(['goodsarr.teacher_form'=>$post['teacher_form']]);
        }
        if(!empty($post['class_day'])){
            $goods_query->andWhere(['gct.class_day'=>$post['class_day']]);
        }


        $category_all = Fun::cache('CateGoodsCourse_cache',false);
        $cat_ids = array_keys($category_all);
        if(!empty($post['cat_id']) && $post['cat_id'] != 'all'){
            $category = Cate::findOne(['id' => $post['cat_id']]);
            $category_childs = $category['arrchild_id'];
            $category_childsarr = explode(",", $category_childs);
            foreach ($category_childsarr as $k => $vv) {
                if (!in_array($vv, $cat_ids)) {
                    unset($category_childsarr[$k]);
                }
            }
            $goods_query->andWhere(['in','g.cat_id',$category_childsarr]);
        }else{
            $category_childsarr = $cat_ids;
        }
        $goods_query->andWhere(['in','g.cat_id',$category_childsarr]);

        if(!empty($post['content'])){
            $goods_query->andWhere(['like', 'g.name', $post['content']]);
        }

        $coures_list =$goods_query->groupBy('g.id')->all(Fun::getSiteDb());
        if($coures_list){
            foreach ($coures_list as $val){
                $d = [];
                $d['goods_id'] = $val['id'];
                $d['goods_name'] = $val['name'];
                $d['teacher_name'] = $val['teacher_name'];
                $d['price'] = $val['price'];
                $d['picture'] = $val['picture'];
                $return[] = $d;
            }
        }
        return new ArrayDataProvider([
            'allModels' => $return,
            'pagination' => [
                'pageSize' => 6
            ],
        ]);

    }

    public function actionCourse_zhangchao($teacher_id){
//        $teacher_id = \Yii::$app->request->get('teacher_id');
        $post = \Yii::$app->request->post();
        if($teacher_id == null){
            return ['statusCode'=>200,'message'=>'error','items'=>'请选登录'];
        }
        $teacher = Teacher::findOne($teacher_id);
        $return = [];
        $model = (new Query())->select(['g.*'])
            ->from('{{%goods}} g')
            ->leftJoin('{{%goods_attr}} ga','g.id = ga.goods_id');
        if(!empty($post['teacher_form'])){
            $model->leftJoin('{{%goods_attr}} gat','g.id = gat.goods_id');
        }
        $model->where(['ga.obj_type'=>'teacher','ga.attribute_id'=>$teacher_id]);
        if(!empty($post['class_day'])){
            $model->leftJoin('{{%goods_class_time}} gct','g.id = gct.goods_id');
            $model->andWhere(['gct.class_day'=>$post['class_day']]);
        }
        if(!empty($post['cat_id']) && $post['cat_id'] != 'all'){
            $model->andWhere(['g.cat_id'=>$post['cat_id']]);
        }
        if(isset($post['teacher_form']) && $post['teacher_form'] != '' &&  $post['teacher_form'] != 'all'){
            $model->andWhere(['gat.obj_type'=>'teacher_form','gat.attribute_id'=>$post['teacher_form']]);
        }
        if(!empty($post['content'])){
            $model->andWhere(['like', 'g.name', $post['content']]);
        }

        $coures_list =$model->groupBy('g.id')->all(Fun::getSiteDb());
        if($coures_list){
            foreach ($coures_list as $val){
                $d = [];
                $d['goods_id'] = $val['id'];
                $d['goods_name'] = $val['name'];
                $d['teacher_name'] = $teacher->name;
                $d['price'] = $val['price'];
                $d['picture'] = $val['picture'];
                $return[] = $d;
            }
        }
        return new ArrayDataProvider([
            'allModels' => $return,
            'pagination' => [
                'pageSize' => 6
            ],
        ]);
    }

    /*
     * 进行会员的搜索操作
     */
    public function actionMembers(){
        $site = \Yii::$app->request->get('site');
        $site_info = (new Query())->select('*')->from('{{%site}}')->where(['domain'=>$site])->one();
        $site_id = $site_info['id'];
        $customers_query = (new Query())->select('c.*')->from('{{%customer}} c');
        $post = \Yii::$app->request->post();
        $customers_query = $customers_query->leftJoin('{{%customer_register_relation}} crr','crr.customer_id = c.id')->where(['crr.site_id'=>$site_id]);
        if(!empty($post['content'])){
            if(preg_match("/^1[34578]\d{9}$/",$post['content'])){//输入的是手机号
                $customers_query->andWhere(['like', 'name', $post['content']]);
            }elseif (preg_match("/^\w{6}$/",$post['content'])){//输入的是激活码
                $cardattr = (new Query())->select(['customer_id'])->from('{{%card_attr}}')->where(['v_code'=>$post['content']])->one();
                if(!empty($cardattr)){
                    $customers_query->andWhere(['c.id'=>$cardattr['customer_id']]);
                }else{
                    $customers_query->andWhere(['c.id'=>'no']);
                }
            }else{
                $customers_query->andWhere(['c.id'=>'no']);
            }
        }
        $customers = $customers_query->all();
        return new ArrayDataProvider([
            'allModels' => $customers,
            'pagination' => [
                'pageSize' => 12
            ],
        ]);
    }
    /*
     * 获取会员详细
     */
    public function actionMemberdetail($member_id){
        if(empty($member_id)){
            return ['statusCode'=>200,'message'=>'error','items'=>'会员id为空'];
        }
        $member = (new Query())->select('*')->from('{{%customer}}')->where(['id'=>$member_id])->one();
        if(empty($member)){
            return ['statusCode'=>200,'message'=>'error','items'=>'不存在该会员'];
        }else{
            $data = [
                'id' => $member['id'],
                'name' => $member['name'],
                'logo' => $member['logo'],
                'mobile' => $member['mobile'],
                'address' => $member['province'].$member['city'].$member['region'],
                'account' => $member['account'],
                'grade_level' => $member['grade_id']!=2?'普通用户':'激活会员'
            ];
            return ['statusCode'=>200,'message'=>'success','items'=>$data];
        }
    }

    public function actionGetcate(){
        $category_all = Fun::cache('CateGoodsCourse_cache',false);
        $tree_all = (new Tree($category_all))->get_tree_sleect_app_array();
//        $tree_all = (new Tree($category_all))->get_tree_app_array();
       // print_r($tree_all);exit;
        $tree_all = array_values($tree_all);
        return ['statusCode'=>200,'message'=>'success','items'=>$tree_all];


    }
    /*
     * 获取账单详细
     */
    public function actionMemberbilldetails($member_id){
        if(empty($member_id)){
            return ['statusCode'=>200,'message'=>'error','items'=>'会员id为空'];
        }
        $orders = (new Query())->select('o.id as id, o.sn as sn, o.pay_status as pay_status, o.created_at as created_at, oi.orders_type_id as orders_type_id,oi.goods_id as goods_id,oi.name as goods_name, oi.amount as amount, oi.discount as discount, oi.actual_scholarship_money as actual_scholarship_money, oi.actual_cash_money as actual_cash_money')->from('{{%orders}} o')->where(['customer_id'=>$member_id])
            ->leftJoin('{{%orders_items}} oi','oi.orders_id = o.id')->where(['o.customer_id'=>$member_id])->orderBy(['o.id' => SORT_DESC])->all(Fun::getSiteDb());
        return ['statusCode'=>200,'message'=>'success','items'=>$orders];
    }

    public function actionMembercourselists($member_id){
        if(empty($member_id)){
            return ['statusCode'=>200,'message'=>'error','items'=>'会员id为空'];
        }
        $orders = Orders::find()->select('{{%orders}}.*,{{%orders_items}}.id as order_itemid,{{%mechanic}}.name as mechanicname,{{%goods}}.name as gname,{{%goods}}.picture as gpicture,
        {{%class_manage}}.status as classmanagestatus,{{%class_manage}}.class_id as classmanageid,{{%orders_items}}.student_id as item_studentid')
            ->joinWith('ordersItems.goods.mechanic')->joinWith('ordersItems.goods.classmanage')->joinWith('ordersItems.goods.goodsClassTime')
            //->joinWith('ordersItems.depositLog')
            ->where(['{{%orders}}.customer_id' => $member_id, '{{%orders}}.is_delete' => 0, '{{%class_manage}}.is_old' => 0])
            ->andWhere(['in','{{%orders}}.order_type',['normal','appointment']])//真心不知道预约的课程到底是appointment还是normal，等后台和微信统一起来再改，现在先写两个吧
            ->andWhere(['in', 'pay_status', [0,1, 4, 5]])->andWhere(['not', ['{{%orders}}.status' => 2]])
            ->asArray()
            ->all(Fun::getSiteDb());

        if(!empty($orders)){
            foreach($orders as $okey=>$order){
                if(!empty($order['ordersItems'])){
                    foreach ($order['ordersItems'] as $oikey=>$orderitem){
                        $depositlog = null;
                        $depositlog = DepositLog::find()->where(['orders_items_id'=>$orderitem['id'],'site_id'=>Fun::getSiteId()])->asArray()->one();
                        if(!empty($depositlog)){
                            $orders[$okey]['ordersItems'][$oikey]['depositLog'][] = $depositlog;
                        }
                    }
                }
            }
        }

        $orderdata = $this->getOrderDetail($orders, $member_id);//现在的逻辑应该是都缴纳保证金了吧……就算不缴纳那保证金的金额应该也是0的
        return ['statusCode'=>200,'message'=>'success','items'=>$orderdata];
    }

    public function getOrderDetail($orderarr, $customer_id)
    {
        if ($orderarr == null || $customer_id == null) {
            return null;
        }
        foreach ($orderarr as $k => $order) {

            $orderitems = $order['ordersItems'];
            if (!empty($orderitems)) {
                $orderitems = $this->getOrderitemsdetail($orderitems,$order);
            }
            $order['ordersItems'] = $orderitems;
            $orderarr[$k] = $order;
        }
        return $orderarr;
    }

    private function getOrderitemsdetail($orderitems = null,$order){
        if(!empty($orderitems)){
            foreach ($orderitems as $kk => $orderitem) {
                $goodsarr = (new Query())->select(["goods_id", "max(IF(name='地域',value,'')) as region1", "max(IF(name='上课时间',value,'')) as start_time", "max(IF(name='下课时间',value,'')) as end_time",
                    "max(IF(name='授课方式',value,'')) as lesson_format", "max(IF(name='上课星期',value,'')) as classday", "max(IF(name='老师',value,'')) as teacher", "max(IF(name='教室名称',value,'')) as classroom_name", "max(IF(name='教室编号',value,'')) as classroom_number", "max(IF(name='是否可预约',value,'')) as is_appointment", "max(IF(name='最满人数',value,'')) as max_number", "max(IF(name='上课次数',value,'')) as class_count"])->from('{{%goods_attr}}')->groupBy('goods_id')
                    ->having(['goods_id' => $orderitem['goods_id']])->one(Fun::getSiteDb());
                $checkout = new CheckoutManage();
                if(isset($orderitem['goods']['goodsClassTime']) && !empty($orderitem['goods']['goodsClassTime'])){
                    $goodsarr['lesson_time'] = $checkout->getGoodsclasstime($orderitem['goods']['goodsClassTime']);
                }else{
                    $goodsarr['lesson_time'] = null;
                }
                $attendclass_num = 0;
                $student_classstatus = '未开课';

                //已经开班了
                if (!empty($orderitem['student_id']) && $orderitem['student_id'] != 0) {
                    $class_student = ClassStudent::find()->where(['class_manage_id' => $order['classmanageid'], 'order_sales_id' => $orderitem['id'], 'student_id' => $orderitem['student_id']])->andWhere(['not', ['status' => 3]])->one();
                    if (!empty($class_student)) {
                        $class_student_id = $class_student['id'];
                        if ($class_student['status'] == 1) {
                            $student_classstatus = '已开课';
                        } elseif ($class_student['status'] == 2 || $class_student['status'] == 4) {
                            $student_classstatus = '已退课';
                        } else {
                            $student_classstatus = '未开课';
                        }
                        $student_classstatus = $student_classstatus;
                        $attendclass_num = StudentCheck::find()->where(['class_student_id' => $class_student_id, 'order_sales_id' => $orderitem['id'], 'status' => 1])->count();
                    }else{//未开课的学员点击之后就是清空class_student
                        $student_classstatus = '已退课';
                    }
                }

                //将当前时间>最近一次的上课时间的课次作为已上课的数量，到时候退课的金额不按照student_check来减，而是根据当前已过课节
                $classlesson_arr = ClassLesson::find()->where(['class_id'=>$order['classmanageid']])->all();
                $time = time();
                $havecrossed_num = 0;
                foreach($classlesson_arr as $classlesson){
                    if($time >= $classlesson->class_date){
                        $havecrossed_num = $havecrossed_num+1;
                    }
                }
                if ($attendclass_num <= floor($goodsarr['class_count'] / 3) && $student_classstatus != '已退课') {//小于课程的1/3课次可以退课
                    $classlesson = ClassLesson::find()->where(['class_id'=>$order['classmanageid'],'lesson'=>floor($goodsarr['class_count'] / 3)+1])->andWhere(['<=','class_date',time()])->one(Fun::getSiteDb());
                    if(!empty($classlesson)){//说明第5节课上课时间已经<当前时间了
                        $can_returncourse = 0;
                    }else{
                        $can_returncourse = 1;
                    }

                } else {
                    $can_returncourse = 0;
                }
                $orderitem['goodarr'] = $goodsarr;
                $orderitem['attendclass_num'] = $attendclass_num;
                $orderitem['havecrossedclass_num'] = $havecrossed_num;//已经过去了的课次数目
                $orderitem['can_returncourse'] = $can_returncourse;
                $orderitem['student_classstatus'] = $student_classstatus;
                $orderitems[$kk] = $orderitem;
            }
        }
        return $orderitems;
    }

    public function actionGoodsinfo($goods_id){
        if($goods_id == null){
            return ['statusCode'=>200,'message'=>'error','items'=>'请选登录'];
        }
        $model = Goods::findOne($goods_id);
        $goodsArr = Fun::getGoodsAttr($goods_id);
        $GoodsArr = [];
        if($model){
            $GoodsArr['id'] = $model->id;
            $GoodsArr['name'] = $model->name;
            $GoodsArr['mechanic_name'] = isset($model->mechanic->name)?$model->mechanic->name:'';
            $GoodsArr['teacher_form'] = isset($goodsArr['teacher_form'])?Util::getModality($goodsArr['teacher_form']):'';
            $GoodsArr['teacher_name'] = isset($goodsArr['teacher'])?$goodsArr['teacher']:'';
            $teacher_id=GoodsAttr::findOne(['goods_id'=>$goods_id,'obj_type'=>'teacher']);
            if($teacher_id != null){
                $teacher=Teacher::findOne(['id'=>$teacher_id->attribute_id]);
            }
            $GoodsArr['teacher_desc'] = isset($teacher->teacher_desc)?$teacher->teacher_desc:'';
            $GoodsArr['teacher_img'] = isset($teacher->img)?$teacher->img:'';
            //课程
            $course_id=GoodsAttr::findOne(['goods_id'=>$goods_id,'obj_type'=>'course']);
            if($course_id!=null){
                $course=Course::findOne(['id'=>$course_id->attribute_id]);
            }
            $GoodsArr['course_desc'] = isset($course->description)?$course->description:'';
            $goodsTime = GoodsClassTime::find()->where(['goods_id'=>$goods_id])->all();
            $arr = [];
            $str =  '';
            if($goodsTime){
                foreach ($goodsTime as $time){
                     $arr[] = Util::getWeek($time->class_day).' '.$time->start_time.'-'.$time->end_time;
                     $str  .= Util::getWeek($time->class_day).Util::getLastAfternoon($time->start_time).' ';
                }
            }
            $GoodsArr['goods_time'] = $arr;
            $GoodsArr['time'] =$str;
            $GoodsArr['max_number'] = isset($goodsArr['max_number'])?$goodsArr['max_number']:'';
            $classmanage = ClassManage::find()->where(['associate_id'=>$goods_id])->one();
            $count = 0;
            if($classmanage != null ){
                $count = ClassStudent::find()->where(['status'=>[1,2],'class_manage_id'=>$classmanage->class_id])->count();
            }
            $GoodsArr['booked'] = $count;
            $goods_group = (new Query())->select('*')->from('{{%goods_group}}')->where(['goods_id'=>$model->id])->one(Fun::getSiteDb());
            if(!empty($goods_group)){
                $art_courses = (new Query())->select('a.*,c.name as course_name,c.description as course_description,tea.name as teacher_name,tea.img as teacher_img,art_tea.class_day as class_day,art_tea.star_time as start_time,art_tea.end_time as end_time')->from('{{%arrangement}} a')
                    ->leftJoin('{{%art_relation}} ar','a.id = ar.arrangement_id')
                    ->leftJoin('{{%course}} c','c.id = a.course_id')
                    ->leftJoin('{{%teacher}} tea','tea.id = a.teacher_id')
                    ->leftJoin('{{%art_teaching}} art_tea','art_tea.id = ar.art_teaching_id')
                    ->where(['ar.art_teaching_id'=>$goods_group['group_id']])->all(Fun::getSiteDb());
                $GoodsArr['art_courses'] = $art_courses;
            }else{
                $GoodsArr['art_courses'] = null;
            }
        }
        return ['statusCode'=>200,'message'=>'success','items'=>$GoodsArr];

    }
}